package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.CategoryDao;
import com.aaa.vegetablemarketexpress.entity.Brand;
import com.aaa.vegetablemarketexpress.entity.Category;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.CategoryService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @FileName: CategoryServiceImpl
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/6 15:07
 * @Version: 1.0.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private HttpSession session; // 获取session

    /**
     * 分页
     * @param page
     * @return
     */
    @Override
    public PageInfo<Category> queryByPage(Page<Category> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageSize == null||pageNo == 0||pageSize == 0) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }
        // 调用PageHelper设置参数方法，开始分页
        PageHelper.startPage(pageNo,pageSize);
        // 获取page类的 data 属性，该属性 是Dept类型
        Category data = page.getData();
        // 调用dao层查询
        List<Category> categoryList = categoryDao.queryByParam(data);
        // 使用查询结果封装成返回对象
        return new PageInfo(categoryList);
    }

    /**
     * id查询
     * @param categoryId
     * @return
     */
    @Override
    public Category queryByCategoryId(Long categoryId) {
        return  categoryDao.queryByCategoryId(categoryId);
    }

    /**
     * 修改
     * @param category
     * @return
     */
    @Override
    public int add(Category category) {
        category.setStatus("0");
        category.setDelFlag("0");
        category.setCreateTime(new Date());
        User userList = (User)session.getAttribute("userInfo");
        String userName = userList.getUserName();
        category.setCreateBy(userName);
        return categoryDao.add(category);
    }

    /**
     * 更新
     * @param category
     * @return
     */
    @Override
    public int update(Category category) {
        category.setUpdateTime(new Date());
        User userList = (User)session.getAttribute("userInfo");
        String userName = userList.getUserName();
        category.setUpdateBy(userName);
        return categoryDao.update(category);
    }

    /**
     * 删除
     * @param categoryId
     * @return
     */
    @Override
    public int deleteBycategoryId(Long categoryId) {
        return categoryDao.deleteByCategoryId(categoryId);
    }

    /**
     * 查询分类树形结构
     * @return
     */
    @Override
    public List<Category> queryCategoryTree() {
        return categoryDao.queryCategoryTree();
    }

}
